package com.demo.apparel.controller.uniapp;

import com.demo.apparel.common.Result;
import com.demo.apparel.dto.ClothesUploadDTO;
import com.demo.apparel.service.uniapp.WxClothesService;
import com.demo.apparel.vo.uniapp.ClothesDetailVO;
import com.demo.apparel.vo.uniapp.SimpleClothesVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/clothes")
@Tag(name = "衣服上传接口", description = "上传衣服图片和信息")
public class WxClothesController {

    @Autowired
    private WxClothesService wxClothesService;

    @PostMapping(value = "/upload/image", produces = "application/json;charset=UTF-8")
    @Operation(summary = "上传衣服图片，返回图片URL和ID")
    public Result<Map<String, String>> uploadImage(@RequestParam("file") MultipartFile file,
                                                   @RequestAttribute("userId") String userId) {
        Map<String, String> result = wxClothesService.uploadImageAndSave(file, userId);
        result.put("msg","success");
        return Result.successes(result,"success");
    }


    @PostMapping("/save/info")
    @Operation(summary = "提交衣服其他信息")
    public Result<Void> saveInfo(@RequestBody ClothesUploadDTO dto,
                                 @RequestAttribute("userId") String userId) {
        wxClothesService.saveClothesInfo(dto, userId);
        return Result.success();
    }

    @GetMapping("/search")
    @Operation(summary = "查询")
    public Result<List<SimpleClothesVO>> searchUserClothes(@RequestAttribute("userId") String userId,
                                                           @RequestParam(required = false) String keyword,
                                                           @RequestParam(required = false) String season) {
        List<SimpleClothesVO> result = wxClothesService.searchUserClothes(userId, keyword, season);
        return Result.success(result);
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "根据ID查询衣物详情")
    public Result<ClothesDetailVO> getClothesDetail(@PathVariable("id") String id,
                                                    @RequestAttribute("userId") String userId) {
        ClothesDetailVO detail = wxClothesService.getClothesDetailById(id, userId);
        return Result.success(detail);
    }

}

